package com.rjj.wlqq.listen;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

/**
 * @author renjianjun
 * @date 2020/8/14 09:27
 * @email 215060580@qq.com
 *
 * spring初始化管理的bean后就会执行这个方法
 *
 * 这里是写了一个把所有的访问路径输出
 *
 */
@Component
public class MyMappingListenProcesser implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Method[] allDeclaredMethods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
        for (Method allDeclaredMethod : allDeclaredMethods) {
            RequestMapping requestMapping = AnnotationUtils.findAnnotation(allDeclaredMethod, RequestMapping.class);
            if(null != requestMapping){
                for (String path : requestMapping.value()) {
                    System.out.println(allDeclaredMethod.getDeclaringClass() + "#" + allDeclaredMethod.getName() + " 访问路径：[" + path + "]");
                }
            }
        }
        return bean;
    }
}
